package com.bto.solar.work.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bto.solar.work.common.BizException;
import com.bto.solar.work.constant.BizCode;
import com.bto.solar.work.dao.ILeasePrjProjectProductService;
import com.bto.solar.work.dao.ILeasePrjProjectService;
import com.bto.solar.work.entity.LeasePrjProject;
import com.bto.solar.work.entity.LeasePrjProjectProduct;
import com.bto.solar.work.utils.DepartmentUtils;
import com.bto.solar.work.utils.NullUtils;
import com.bto.solar.work.utils.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shiyi
 */
@Service
public class BtoLeasePrjProjectService {

    @Autowired
    private ILeasePrjProjectService iLeasePrjProjectService;

    @Autowired
    private ILeasePrjProjectProductService iLeasePrjProjectProductService;


    public PageInfo<LeasePrjProject> getLeasePrjProjectList(Integer page , Integer limit){
        PageHelper.startPage(page,limit);
        List<LeasePrjProject> leasePrjProjectProductList = iLeasePrjProjectService.list();
        return PageInfo.of(leasePrjProjectProductList);
    }

    public void updateLeasePrjProject(Integer id , LeasePrjProject leasePrjProject){
        LambdaQueryWrapper<LeasePrjProject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeasePrjProject::getId , id);
        LeasePrjProject one = iLeasePrjProjectService.getById(id);
        if (NullUtils.isNull(one)){
            throw new BizException("无数据");
        }
        boolean update = iLeasePrjProjectService.update(leasePrjProject, wrapper);
        if (!update){
            throw new BizException("更新失败");
        }
    }



    /**
    * 产品方案——页面展示
    * */
    public PageInfo<LeasePrjProjectProduct> getLeasePrjProjectProductList(Integer page , Integer limit) {
        PageHelper.startPage(page,limit);
        LambdaQueryWrapper<LeasePrjProjectProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(LeasePrjProjectProduct::getCreateTime);
        List<LeasePrjProjectProduct> list = iLeasePrjProjectProductService.list(wrapper);
        list.stream().forEach(leasePrjProjectProduct -> {
            try {
                DepartmentUtils.copyLeasePrjProjectProduct(leasePrjProjectProduct);
            } catch (JsonProcessingException e) {
                throw new BizException(BizCode.JSON_ERROR);
            }
        });

        PageInfo<LeasePrjProjectProduct> info = new PageInfo<>(list);
        return info;
    }

    /**
     * 产品方案——下拉框显示
     * */
    public List<LeasePrjProjectProduct> getLeasePrjProjectProductList() {
        List<LeasePrjProjectProduct> list = iLeasePrjProjectProductService.list();
        list.forEach(leasePrjProjectProduct -> {
            try {
                DepartmentUtils.copyLeasePrjProjectProduct(leasePrjProjectProduct);
            } catch (JsonProcessingException e) {
                throw new BizException(BizCode.JSON_ERROR);
            }
        });
        return list;
    }


    /**
    * 产品方案——新增
    * */
    public void addLeasePrjProjectProduct(LeasePrjProjectProduct leasePrjProjectProduct) {
        if (StringUtils.isEmpty(leasePrjProjectProduct.getCode())){
            throw new BizException("产品码为空");
        }
        if (StringUtils.isEmpty(leasePrjProjectProduct.getAreaType())){
            throw new BizException("产品类型为空");
        }
        if (StringUtils.isEmpty(leasePrjProjectProduct.getArea())){
            throw new BizException("地区为空");
        }

        LeasePrjProjectProduct leasePrjProjectProductByDb = iLeasePrjProjectProductService.lambdaQuery()
                .eq(LeasePrjProjectProduct::getCode, leasePrjProjectProduct.getCode()).one();
        if (NullUtils.isNotNull(leasePrjProjectProductByDb)) {
            throw new BizException("产品编号{} 已存在，无法添加" , leasePrjProjectProductByDb.getCode());
        }

        boolean save = iLeasePrjProjectProductService.save(leasePrjProjectProduct);

        if (!save){
            throw new BizException("保存失败");
        }
    }

    /**
    * 产品方案——修改
    * */
    public void updateLeasePrjProjectProduct(Integer id , LeasePrjProjectProduct leasePrjProjectProduct){
        LambdaQueryWrapper<LeasePrjProjectProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeasePrjProjectProduct::getId , id);
        LeasePrjProjectProduct oldLeaseProduct = iLeasePrjProjectProductService.getOne(wrapper);

        if(NullUtils.isNull(oldLeaseProduct)){
            throw new BizException("没有改产品，无法更新");
        }
        if (!oldLeaseProduct.getCode().equals(leasePrjProjectProduct.getCode())){
            throw new BizException("产品码不能更新");
        }
        if (!oldLeaseProduct.getAreaType().equals(leasePrjProjectProduct.getAreaType())){
            throw new BizException("产品类型不能更新");
        }
        boolean update = iLeasePrjProjectProductService.update(leasePrjProjectProduct, wrapper);
        if (!update){
            throw new BizException("更新失败");
        }
    }


}
